'use strict';
const db = uniCloud.database();
const fbCollection = db.collection('xxf-feedback');
const dbCmd = db.command;
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event);
	const {
		uid,
		action
	} = event;
	if (!uid && action) {
		return {
			msg: '用户ID参数错误！',
			code: 0,
			data: null
		};
	}
	switch (action) {
		case 'createFeedback':
			return createFeedback(event);
		default:
			return {
				msg: 'Invalid action！',
					code: 0,
					data: null
			};
	}
};
async function createFeedback(params) {
	const {
		uid,
		title,
		content,
	} = params;
	const addRes = await fbCollection.add({
		uid,
		title,
		content,
		createTime: Date.now(),
	})
	console.log("addRes: ", addRes);
	if (addRes && addRes.id) {
		return {
			msg: 'success',
			code: 1,
			data: {
				id: addRes.id,
			}
		};
	} else {
		return {
			msg: '反馈失败！',
			code: 0,
			data: null
		};
	}
}